package servlets.ads;

import java.io.IOException;
import java.net.URL;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jsoup.Jsoup;

public class GetTiVendoIoADs extends HttpServlet{

	private static final long serialVersionUID = -1939323370632501117L;
	private static final String URLBASE = "http://localhost:8080/DatabaseRESTWebService/GetADs";
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) {
		String answer = null, starterId, limit, adType, method;
		URL url;
		starterId = request.getParameter("starterId");
		method = request.getParameter("method");
		limit = request.getParameter("limit");
		adType = request.getParameter("adType");
		
		try {
			url = new URL(URLBASE + "?limit=" + limit + "&starterId=" + starterId + "&method=" + method + "&adType=" + adType);
			answer = Jsoup.connect(url.toString()).ignoreContentType(true).execute().body();
			if(answer.equals("null"))
				throw new Exception();
			
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		
		try {
			//write the result as JSon
			response.getWriter().write(answer);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}